package com.atguigu.jxc.controller;

import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SupplierController {

    @Autowired
    private SupplierService supplierService;


    @PostMapping("/supplier/list")
    public Map<String, Object> list(Integer page, Integer rows, @RequestParam(value = "supplierName", required = false) String supplierName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", supplierService.count());
        int offset = page == 0 ? 1 : page;
        map.put("rows", supplierService.list(new LambdaQueryWrapper<Supplier>()
                .eq(StringUtil.isEmpty(supplierName), Supplier::getSupplierName, supplierName)
                .last("limit " + offset + ", " + rows)));
        return map;
    }
}
